﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.Prompt;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCL.UI.UpDownload
{
    public partial class UploadView : EFViewCmes
    {
        public UploadView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (this.upload.IsBusy)
            {
                this.ValidateMessagePopup("文件正在上传中，请稍候再提交数据！");//弹出提示信息                
                this.TempButtonOpen(true);//确定按钮生效
                return;
            }

            EIInfo eiInfo = new EIInfo();
            eiInfo.Attributes.Add(EIConstants.ServiceNameKey, this.eServiceName);
            eiInfo.Attributes.Add(EIConstants.MethodNameKey, this.eMethodName);
            eiInfo.Attributes.Add("fdKeyId", fdKeyId.Value);
            eiInfo.Attributes.Add("fdName", fdName.Value);
            eiInfo.Attributes.Add("fdDepartment", fdDepartment.Value);
            eiInfo.Attributes.Add("fdWorkSchedule", fdWorkSchedule.Value);
            Dictionary<string, string> dic = upload.InsertFileArgs;
            EIBlock Block = eiInfo.AddBlock("insertfiles");
            EIColumn dir = new EIColumn();
            dir.Name = "fdKeyId";
            dir.TypeLabel = "C";
            Block.AddColumn(dir);

            dir = new EIColumn();
            dir.Name = "fdMemoryName";
            dir.TypeLabel = "C";
            Block.AddColumn(dir);

            dir = new EIColumn();
            dir.Name = "fdForeignId";
            dir.TypeLabel = "C";
            Block.AddColumn(dir);

            foreach (string obj in dic.Keys)
            {
                EIRow row = new EIRow();
                row.Add("fdKeyId", dic[obj]);
                row.Add("fdMemoryName", obj);
                row.Add("fdForeignId", "");
                Block.AddRow(row);
            }
            base.Submit(eiInfo, DoUploadCompleted);
        }

        /// <summary>
        /// 上传完毕后的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoUploadCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status != -1)
            {
                CmesWindows _window = this.Parent as CmesWindows;
                _window.DialogResult = false;
                _window.Close();
            }
            else
            {
                this.ValidateMessagePopup(e.EIInfo.GetMsg());
                this.TempButtonOpen(true);
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }


        /// <summary>
        /// 验证失败，消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }
    }
}
